﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Serilog;
using Serilog.Sinks.Elasticsearch;

namespace LogTest.Setup
{
    public static class SerilogSetup
    {

        public static void RegiestSerilog(this WebApplicationBuilder builder)
        {


            builder.Host.UseSerilog((context, config) =>
            {
                config.MinimumLevel.Information()
                           .Enrich.FromLogContext()
                           .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://123.60.139.100:9200"))
                           {
                               IndexFormat = "logstash-{0:yyyy.MM}",
                               EmitEventFailure = EmitEventFailureHandling.RaiseCallback,
                               FailureCallback = e => Console.WriteLine("Unable to submit event " + e.MessageTemplate),
                               AutoRegisterTemplate = true,
                               AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                               NumberOfShards=2,
                               NumberOfReplicas=2,
                               ModifyConnectionSettings =
                                   conn =>
                                   {
                                       conn.ServerCertificateValidationCallback((source, certificate, chain, sslPolicyErrors) => true);
                                       //conn.BasicAuthentication("elastic", "U3G44HpMwQv3Y5tq916TyV74");
                                       return conn;
                                   }
                           })
                           .WriteTo.Console();
            });

        }


    }
}
